package com.xjscrm.console.web.groupSend;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xjscrm.common.entity.CorpApplication;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.param.groupSend.*;
import com.xjscrm.console.service.common.corpapplication.CorpApplicationService;
import com.xjscrm.console.service.customer.groupSend.BatchSpeedGroupSendService;
import com.xjscrm.console.service.customer.groupSend.SpeedGroupSendService;
import com.xjscrm.console.vo.SpeedGroupSendJobVo;
import com.xjscrm.console.vo.SpeedGroupSendTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Api(value = "极速群发管理", tags = "极速群发管理yao")
@RestController
@RequestMapping(value = "/speedGroupSend")
public class SpeedGroupSendController {

    @Autowired
    private SpeedGroupSendService speedGroupSendService;
    @Autowired
    private BatchSpeedGroupSendService batchSpeedGroupSendService;
    @Autowired
    private CorpApplicationService corpApplicationService;

    @ApiOperation(value = "新增极速群发任务", notes = "新增极速群发任务", response = Boolean.class)
    @PostMapping(value = "/addSpeedGroupSendTask")
    @ApiOperationSupport(order = 1)
    public Object addSpeedGroupSendTask(@RequestBody SpeedGroupSendTaskDto speedGroupSendTaskDto) throws Exception {
        String corpId = speedGroupSendTaskDto.getCorpId();
        CorpApplication corpApplication = corpApplicationService.getByCorpId(corpId);
        if (Objects.isNull(corpApplication) || org.apache.commons.lang3.StringUtils.isAnyBlank(corpApplication.getAuthCorpId(), corpApplication.getPermanentCode())) {
            throw new BusinessException("创建极速群发失败: 企微未授权");
        }
        speedGroupSendService.addSpeedGroupSendTask(speedGroupSendTaskDto);
        return true;
    }

    @ApiOperation(value = "修改极速群发任务", notes = "修改极速群发任务", response = Boolean.class)
    @PostMapping(value = "/updateSpeedGroupSendTask")
    @ApiOperationSupport(order = 2)
    public Object updateSpeedGroupSendTask(@RequestBody SpeedGroupSendTaskDto speedGroupSendTaskDto) throws Exception {
        speedGroupSendService.updateSpeedGroupSendTask(speedGroupSendTaskDto);
        return true;
    }

    @ApiOperation(value = "获取极速群发任务中心列表", notes = "获取极速群发任务中心列表", response = SpeedGroupSendTaskVo.class)
    @PostMapping(value = "/getSpeedGroupSendTaskList")
    @ApiOperationSupport(order = 3)
    public Object getSpeedGroupSendTaskList(@RequestBody SpeedGroupSendTaskQuery speedGroupSendTaskQuery) throws Exception {
        return speedGroupSendService.getSpeedGroupSendTaskList(speedGroupSendTaskQuery);
    }

    @ApiOperation(value = "获取极速群发任务中心任务详情", notes = "获取极速群发任务中心任务详情", response = SpeedGroupSendTaskVo.class)
    @PostMapping(value = "/getSpeedGroupSendTaskDetail")
    @ApiOperationSupport(order = 4)
    public Object getSpeedGroupSendTaskDetail(@RequestBody SpeedGroupSendTaskDto speedGroupSendTaskDto) throws Exception {
        return speedGroupSendService.getSpeedGroupSendTaskDetail(speedGroupSendTaskDto);
    }

    @ApiOperation(value = "开启或关闭极速群发任务", notes = "开启或关闭极速群发任务", response = Boolean.class)
    @PostMapping(value = "/startAndStop")
    @ApiOperationSupport(order = 5)
    public Object startAndStop(@RequestBody StartAndStopDto startAndStopDto) throws Exception {
        speedGroupSendService.startAndStop(startAndStopDto);
        return true;
    }

    @ApiOperation(value = "删除极速群发任务", notes = "删除极速群发任务", response = Boolean.class)
    @PostMapping(value = "/deleSpeedGroupSendTask")
    @ApiOperationSupport(order = 6)
    public Object deleSpeedGroupSendTask(@RequestBody SpeedGroupSendTaskDto speedGroupSendTaskDto) throws Exception {
        speedGroupSendService.deleSpeedGroupSendTask(speedGroupSendTaskDto);
        return true;
    }


    @ApiOperation(value = "获取极速群发任务列表", notes = "获取极速群发任务列表", response = SpeedGroupSendJobVo.class)
    @PostMapping(value = "/getSpeedGroupSendJobList")
    @ApiOperationSupport(order = 7)
    public Object getSpeedGroupSendJobList(@RequestBody SpeedGroupSendJobQuery speedGroupSendJobQuery) throws Exception {
        return speedGroupSendService.getSpeedGroupSendJobList(speedGroupSendJobQuery);
    }


    @ApiOperation(value = "获取极速群发任务列表详情", notes = "获取极速群发任务列表详情", response = SpeedGroupSendJobVo.class)
    @PostMapping(value = "/getSpeedGroupSendJobDetail")
    @ApiOperationSupport(order = 8)
    public Object getSpeedGroupSendJobDetail(@RequestBody SpeedGroupSendJobDto SpeedGroupSendJobDto) throws Exception {
        return speedGroupSendService.getSpeedGroupSendJobDetail(SpeedGroupSendJobDto);
    }


    @ApiOperation(value = "批量取消任务", notes = "批量取消任务", response = Boolean.class)
    @PostMapping(value = "/batchcancelSpeedGroupSendJob")
    @ApiOperationSupport(order = 9)
    public Object batchcancelSpeedGroupSendJob(@RequestBody BatchGroupSendJobDto batchGroupSendJobDto) throws Exception {
        batchSpeedGroupSendService.batchcancelSpeedGroupSendJob(batchGroupSendJobDto);
        return true;
    }

    @ApiOperation(value = "批量删除任务", notes = "批量删除任务", response = Boolean.class)
    @PostMapping(value = "/batchDeleSpeedGroupSendJob")
    @ApiOperationSupport(order = 10)
    public Object batchDeleSpeedGroupSendJob(@RequestBody BatchGroupSendJobDto batchGroupSendJobDto) throws Exception {
        batchSpeedGroupSendService.batchDeleSpeedGroupSendJob(batchGroupSendJobDto);
        return true;
    }

    @ApiOperation(value = "批量确认任务", notes = "批量确认任务", response = Boolean.class)
    @PostMapping(value = "/batchConfirmSpeedGroupSendJob")
    @ApiOperationSupport(order = 11)
    public Object batchConfirmSpeedGroupSendJob(@RequestBody BatchGroupSendJobDto batchGroupSendJobDto) throws Exception {
        batchSpeedGroupSendService.batchConfirmSpeedGroupSendJob(batchGroupSendJobDto);
        return true;
    }

    @ApiOperation(value = "获取符合条件的客户数", notes = "获取符合条件的客户数", response = Integer.class)
    @PostMapping(value = "/getFilterSendTargetCustomer")
    @ApiOperationSupport(order = 11)
    public Object getFilterSendTargetCustomer(@RequestBody SpeedGroupSendTaskDto speedGroupSendTaskDto) throws Exception {
        return speedGroupSendService.getFilterSendTargetNum(speedGroupSendTaskDto);
    }
}
